<?php
/**
 * Set up Environment
 *
 * @package OWL
 * @subpackage Main
 * @since 1.0
 */

/**
 * Checking server environment
 */

// Fix for IIS, which doesn't set REQUEST_URI
if (empty($_SERVER['REQUEST_URI'])) {

	// IIS Mod-Rewrite
	if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
		$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
	}
	// IIS Isapi_Rewrite
	else if (isset($_SERVER['HTTP_X_REWRITE_URL'])) {
		$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL'];
	}
	else
	{
		// Use ORIG_PATH_INFO if there is no PATH_INFO
		if (!isset($_SERVER['PATH_INFO']) && isset($_SERVER['ORIG_PATH_INFO']) )
			$_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];

		// Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
		if (isset($_SERVER['PATH_INFO'])) {
			if ( $_SERVER['PATH_INFO'] == $_SERVER['SCRIPT_NAME'] )
				$_SERVER['REQUEST_URI'] = $_SERVER['PATH_INFO'];
			else
				$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
		}

		// Append the query string if it exists and isn't null
		if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
			$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
		}
	}
}

// Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests
if (isset($_SERVER['SCRIPT_FILENAME']) && (strpos($_SERVER['SCRIPT_FILENAME'], 'php.cgi') == strlen($_SERVER['SCRIPT_FILENAME']) - 7 ) )
	$_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];

// Fix for Dreamhost and other PHP as CGI hosts
if (strpos($_SERVER['SCRIPT_NAME'], 'php.cgi') !== false)
	unset($_SERVER['PATH_INFO']);

// Fix empty PHP_SELF
$PHP_SELF = $_SERVER['PHP_SELF'];
if (empty($PHP_SELF))
	$_SERVER['PHP_SELF'] = $PHP_SELF = preg_replace("/(\?.*)?$/",'',$_SERVER["REQUEST_URI"]);

if (version_compare( '5.0', phpversion(), '>=' )) {
	die('OWL needs PHP version 5.0 or above');
}

if (DEBUG_ENABLE) {
    if (defined("E_DEPRECATED")) {
        error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
    }
    else {
        error_reporting(E_ALL & ~E_NOTICE);
    }
}
else {
    error_reporting(E_ERROR);
}

/**
 * Autoloading core classes
 */

set_include_path(get_include_path().
    PATH_SEPARATOR.CLASS_PATH.
    PATH_SEPARATOR.WIDGETS_PATH.
    PATH_SEPARATOR.MODEL_PATH
);

require_once (CLASS_PATH."owl-format.php");
require_once (CLASS_PATH."owl-loader.php");
Owl_Loader::register_autoload();

/**
 * Including core API
 */
require LIBS_PATH . "kses.php";
require LIBS_PATH . "Browser.php";
require LIBS_PATH . "json.php";
require LIBS_PATH . "cssparser.php";
require "default-scripts.php";
require "default-styles.php";

/**
 * Register global objects
 */
$owl_option = new Owl_Option();

$user = Owl_User::get_instance();

/**
 * Create session
 */
Owl_Http::create_session();

$user->setup_user();

$i18n = new Owl_i18n();
$locale = new Owl_Locale();

$rewrite_engine = new Owl_Rewrite();
register_default_rules();

$owl_widget_control = new Owl_Widget_Factory();
register_default_widgets();
$owl_sidebar_control = new Owl_Sidebar();

$place_holder_control = new Owl_Place_Holder();
$navigation = Owl_Navigation::get_instance();

/**
 * Load theme functions
 */
Owl_Theme_Control::load_theme_functions();

